GameMaker Studio 2 has a number of dedicated functions that can be used to detect both analogue and digital controls from multiple connected game pads. These functions work similar to the Device Inputs, in that you can detect up to four different XInput game pads that are connected (and up to 8 DirectInput gamepads) and deal with the input from each one using the same functions. Note that when a gamepad is plugged in to your device (or it is removed) then an asynchronous System Event is triggered where you can deal with the situation using the appropriate functions.
The gamepad "slots" are indexed from 0 with slots 0 - 3 inclusive being only for Xinput gamepads, ie: Xbox360 controllers and compatibles. However you can also check slots 4 - 11 inclusive for DirectInput gamepads, which means you can detect many other models of controller when connected through these slots. It is worth noting that when using DirectInput gamepads, the constants given below may not match exactly the buttons that you expect when they are pressed, due to the fragmented and non-standardised way that the API is implemented by controller manufacturers. Because of this, it is recommend that you have some kind of gamepad setup screen in your games where people can redefine the gamepad buttons based on input from any connected to device to mitigate any issues (there are gamepad "mapping" functions that can help with this on Windows Desktop, Ubuntu, macOS, and Android targets, while on all others you would need to do this yourself using code). Also note that Direct Input gamepads are run in cooperative mode which means that your game only has access to them when it is the foreground application, which in turn will cause Direct Input controllers to be "lost" if the game loses focus and then "found" again when it comes back into focus (this can be detected in the System Event and dealt with).
When working with the gamepad functions, input can come from axis, buttons and/or hats, which GameMaker Studio 2 will assign to some or all of the following built-in constants (note that "hats" are generally only detected on non-standard controllers):
Constant | Description |
---|---|
gp_face1 | Top button 1 (this maps to the "A" on an Xbox 360 controller and the cross on a PS controller) |
gp_face2 | Top button 2 (this maps to the "B" on an Xbox 360 controller and the circle on a PS controller) |
gp_face3 | Top button 3 (this maps to the "X" on an Xbox 360 controller and the square on a PS controller) |
gp_face4 | Top button 4 (this maps to the "Y" on an Xbox 360 controller and the triangle on a PS controller) |
gp_shoulderl | Left shoulder button |
gp_shoulderlb | Left shoulder trigger |
gp_shoulderr | Right shoulder button |
gp_shoulderrb | Right shoulder trigger |
gp_select | The select button (this is the PS button on a DS4 controller) |
gp_start | The start button (this is the "options" button on a PS4 controller) |
gp_stickl | The left stick pressed (as a button) |
gp_stickr | The right stick pressed (as a button) |
gp_padu | D-pad up |
gp_padd | D-pad down |
gp_padl | D-pad left |
gp_padr | D-pad right |
gp_axislh | Left stick horizontal axis (analogue) |
gp_axislv | Left stick vertical axis (analogue) |
gp_axisrh | Right stick horizontal axis (analogue) |
gp_axisrv | Right stick vertical axis (analogue) |
To better understand exactly what part of the controller each
constant represents, you can refer to the following image of a
standard Xinput gamepad:
Below you can find a list of all the gamepad functions:
- gamepad_is_supported
- gamepad_is_connected
- gamepad_iget_guid
- gamepad_get_device_count
- gamepad_get_description
- gamepad_get_button_threshold
- gamepad_get_axis_deadzone
- gamepad_get_option
- gamepad_set_button_threshold
- gamepad_get_axis_deadzone
- gamepad_set_vibration
- gamepad_set_colour
- gamepad_set_option
- gamepad_axis_count
- gamepad_axis_value
- gamepad_button_check
- gamepad_button_check_pressed
- gamepad_button_check_released
- gamepad_button_count
- gamepad_button_value
- gamepad_hat_count
- gamepad_hat_value
The following gamepad functions also exist and are used for remapping the built in constants to the direct physical inputs of a given gamepad. These functions are only for the Windows Desktop, Ubuntu, macOS, and Android target platforms and on Windows, they will only be valid for Direct input devices. While GameMaker Studio 2 comes with mappings for a number of different gamepads based on SDL Gamepad Controller DB, however due to the huge number of controller types and brands out there, it is impossible to map the GML constants to the correct inputs for every make and model, so with these functions you have the possibility to create your own custom mappings.
The following list shows current compatibility across the
platforms (note that this will change with future updates):
sudo apt-get install jstest-gtk
sudo apt-get install joystick.
This will install GUI support for the joystick/gamepad as well
as the joystick/gamepad API itself. Remapping of controller
constants is not permitted.
Ideally, on all target platforms, you want to enumerate a list of available gamepad "slots" and then check them to see if any devices are detected, something like this:
var maxpads = gamepad_get_device_count();for
(var i = 0; i < maxpads; i++)
{
if (gamepad_is_connected(i))
{
// do stuff with
pad "i"
}
}